﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Search.TxMainSearchControl.TxMainSearchControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls.WPFControls.SearchControl;


namespace DnProcessSimulateCommands.Search.TxMainSearchControl;

public class TxMainSearchControl : UserControl, IComponentConnector
{
  private ObservableCollection<TxSearchBaseCollectionModel> _mainSearchModels;
  private bool _ignoreRegistrySettings;
  private TxWindow _window;
  internal TxSearchControl txsSearch;
  private bool _contentLoaded;

  public ObservableCollection<TxSearchBaseCollectionModel> DataSource
  {
    private set
    {
      this._mainSearchModels = value;
      this.txsSearch.DataSource = this._mainSearchModels;
    }
    get => this._mainSearchModels;
  }

  public event ESCEventHandler OnEsc;

  public event PostSelectionEventHandler OnPostSelectionChanged;

  public TxMainSearchControl() => this.InitializeComponent();

  private void txsSearch_Loaded(object sender, RoutedEventArgs e) => this.GenerateDataModels();

  internal void DefineParentWindow(TxWindow window) => this._window = window;

  public void GenerateDataModels() => this.GenerateDataModels(false);

  public void GenerateDataModels(bool enableAllModels)
  {
    bool commandSearchIsActive = true;
    bool objectsSearchIsActive = true;
    if (!enableAllModels)
      TxSearchModelUtils.LoadFromReg(this._window, out commandSearchIsActive, out objectsSearchIsActive);
    else
      this._ignoreRegistrySettings = true;
    TxSearchITxObjectCollectionModel objectCollectionModel = new TxSearchITxObjectCollectionModel();
    TxSearchCommandCollectionModel commandCollectionModel = new TxSearchCommandCollectionModel();
    objectCollectionModel.Group = TxMainSearchControlStringTable.ObjectsGroupCaption;
    objectCollectionModel.Name = "ObjectsSearch";
    objectCollectionModel.IsActive = objectsSearchIsActive;
    commandCollectionModel.Group = TxMainSearchControlStringTable.CommandsGroupCaption;
    commandCollectionModel.Name = "CommandsSearch";
    commandCollectionModel.IsActive = commandSearchIsActive;
    ObservableCollection<TxSearchBaseCollectionModel> observableCollection = new ObservableCollection<TxSearchBaseCollectionModel>();
    observableCollection.Add((TxSearchBaseCollectionModel) commandCollectionModel);
    observableCollection.Add((TxSearchBaseCollectionModel) objectCollectionModel);
    this.DataSource = observableCollection;
  }

  public void RunSearch(string str) => this.txsSearch.RunSearch(str);

  private void txsSearch_OnEsc(object source, EventArgs e) => this.OnEsc(source, e);

  private void txsSearch_OnPostSelectionChanged(object source, EventArgs e)
  {
    this.txsSearch.RefreshList();
    if (TxSearchModelUtils.WasHandled(source))
      return;
    this.OnPostSelectionChanged(source, e);
  }

  private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  {
    if (this._ignoreRegistrySettings)
      return;
    bool commandSearchIsActive = false;
    bool objectsSearchIsActive = false;
    foreach (TxSearchBaseCollectionModel baseCollectionModel in (Collection<TxSearchBaseCollectionModel>) this.DataSource)
    {
      if (baseCollectionModel.Name == "CommandsSearch")
        commandSearchIsActive = baseCollectionModel.IsActive;
      else if (baseCollectionModel.Name == "ObjectsSearch")
        objectsSearchIsActive = baseCollectionModel.IsActive;
    }
    TxSearchModelUtils.StoreToReg(this._window, commandSearchIsActive, objectsSearchIsActive);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/search/txmainsearchcontrol/txmainsearchcontrol.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    if (connectionId != 1)
    {
      if (connectionId == 2)
      {
        this.txsSearch = (TxSearchControl) target;
        ((FrameworkElement) this.txsSearch).Loaded += new RoutedEventHandler(this.txsSearch_Loaded);
        this.txsSearch.OnEsc += new ESCEventHandler(this.txsSearch_OnEsc);
        this.txsSearch.OnPostSelectionChanged += new PostSelectionEventHandler(this.txsSearch_OnPostSelectionChanged);
      }
      else
        this._contentLoaded = true;
    }
    else
      ((FrameworkElement) target).Unloaded += new RoutedEventHandler(this.UserControl_Unloaded);
  }
}
